﻿using System;
using Internal.KuiHuaBaoDian.Services.Lockstep;

namespace KuiHuaBaoDian.Services.Lockstep {

    /// <summary>
    /// 计时器取消
    /// </summary>
    public class LockstepTimerCancellationToken : KHBD_Object, ILockstepTimerCancellationTokenInternal {

        private event ILockstepTimerCancellationTokenInternal.CancelDelegate CancelEvent;
        event ILockstepTimerCancellationTokenInternal.CancelDelegate ILockstepTimerCancellationTokenInternal.Cancel {
            add { CancelEvent += value; }
            remove { CancelEvent -= value; }
        }

        /// <summary>
        /// 取消
        /// </summary>
        public void Cancel() {
            if (IsDisposed) {
                return;
            }
            CancelEvent?.Invoke();
        }

        protected override void Dispose() {
            CancelEvent = null;
        }

        /// <summary>
        /// 释放
        /// </summary>
        public void Release() => (this as IDisposable).Dispose();
    }
}

namespace Internal.KuiHuaBaoDian.Services.Lockstep {

    public interface ILockstepTimerCancellationTokenInternal : IObject, IDisposable {

        public delegate void CancelDelegate();

        event CancelDelegate Cancel;
    }
}
